package cn.org.xuanguang.kepler.hormos.rdb.jpa.generation;


import cn.org.xuanguang.kepler.core.extension.SPI;
import cn.org.xuanguang.kepler.util.UUIDUtil;

import java.security.SecureRandom;
import java.util.Date;

/**
 * @param <T> 生成值
 */
@SPI(value = "default_id")
public class DefalaultGenerationValues<T> implements GenerationValues<T> {

    @Override
    public Object getValue(String type) {
        if (String.class.getTypeName().equals(type)) {
            return UUIDUtil.uuid();
        }
        if (Long.class.getTypeName().equals(type)) {
            return getSecureRandom();
        }
        if (Integer.class.getTypeName().equals(type)) {
            return getSecureRandom();
        }
        return UUIDUtil.uuid();
    }

    private Long getSecureRandom() {
        SecureRandom random = new SecureRandom();
        long timestamp = new Date().getTime();
        int randomInt = random.nextInt(1000);
        return timestamp + randomInt;
    }

    @Override
    public String getGenerationId() {
        return "default_id";
    }
}
